package com.example.demo4.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "loginFilter",urlPatterns = "/sys/*")
public class LoginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest httpServletRequest = (HttpServletRequest)servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse)servletResponse;

        Object userSession = httpServletRequest.getSession().getAttribute("USER_SESSION");
        if(userSession == null){
            httpServletResponse.sendRedirect(httpServletRequest.getServletContext().getContextPath() + "/Login.jsp");
        }

        filterChain.doFilter(servletRequest,servletResponse);
    }
}
